using UnityEngine;
using System.Collections;

public class Canabalt : MonoBehaviour {

	public Axis _AxisJump;
	public Axis _AxisRight;
	public bool podeAndar;
	public Transform cam;
	public GameObject start;
	public GameObject loser;
	public GameObject youWin;
	public GameObject utilitarista;
	public GameObject retributivista;
	tk2dAnimatedSprite anim;
	bool dead;
	bool win;
	int count;
	bool isUtilitarista;
	void Start()
	{
		anim = GetComponent<tk2dAnimatedSprite>();	
	}
	
	void Update () 
	{
		if(podeAndar)
		{
			if(!_AxisRight.isGrounded)
			{
				float posX = transform.position.x + (3.1f*Time.deltaTime);
				transform.position = new Vector3(posX,transform.position.y,transform.position.z);
			}

			if(Input.GetKeyDown("up") || Input.GetKeyDown("w"))
			{
				if(_AxisJump.isGrounded)
				{
					Jump();
				}
			}
			if(cam.position.x < 69 && cam.position.y > 0)
			{
				float camX = cam.position.x + (3.0f*Time.deltaTime);
				cam.position = new Vector3(camX,cam.position.y,cam.position.z);
			}
			
			else if(transform.position.x < 72)
			{
				podeAndar = false;
				win = true;
				anim.Stop();
				anim.SetFrame(0);
			}
			
			if(transform.position.x < cam.position.x - 3.43f || transform.position.y < 0)
			{
				podeAndar = false;
				dead = true;
				anim.Stop();
				loser.active = true;
			}
			
		}
		
		if(Input.GetKeyDown("space") && !podeAndar && !dead && !win)
		{
			podeAndar = true;
			anim.Play("WalkTim");
			start.active = false;
		}
		if(Input.GetKeyDown("space") && dead)
		{
			transform.position = new Vector3(-4,0.7f,0);
			cam.position = new Vector3(-3,2.4f,-10);
			loser.active = false;
			start.active = true;
			dead = false;
			
		}
		if(win && count < 1)
		{
			youWin.SetActiveRecursively(true);
			if(isUtilitarista)
			{
				utilitarista.active = true;
			}
			else
			{
				retributivista.active = true;
			}
			
			count++;
		}
		
	}
	
	void Jump()
	{
		rigidbody.AddForce(0,3,0,ForceMode.Impulse);
	}
}
